import requests
import re
import csv

url='https://s.weibo.com/top/summary'
headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
    'Cookie':'UOR=,,www.baidu.com; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WWb4TilF7Z8Wui3mM7gqxVA5JpX5KMhUgL.Foq01h-41KnR1h.2dJLoI7poIs809PzEeh2NeK5t; SCF=AjMSQC4qIwWOvcaHhD0p1zqolaYNlfiiJEiUTgCIwFeiZIeOb76mH3Q0f-w60za_-bzlZMqWad96dAgtXnlg86E.; SINAGLOBAL=6915867831016.315.1713804036950; ALF=1720945452; SUB=_2A25Lb458DeRhGeBN41cY-SbEwzWIHXVoBI-0rDV8PUJbkNANLVrekW1NRA_EKW1YWJ-_QnJudO8sozjmTc0fecQe; _s_tentry=weibo.com; Apache=6863395231135.77.1718363227230; ULV=1718363227253:20:1:1:6863395231135.77.1718363227230:1717159350730; ariaReadtype=1; ariaMouseten=null; ariaStatus=false'
}

html=requests.get(url=url,headers=headers)

html.encoding=html.apparent_encoding
res=html.text
reg='<td class="td-01 ranktop ranktop\d*">(.*?)<\/td>\s*<td class="td-02">\s*<a href="(.*?)"\s*target="_blank">(.*?)<\/a>'
content=re.findall(reg,res,re.S)

ids=[id[0] for id in content]
links=['https://s.weibo.com/' + url[1] for url in content]
titles=[title[2] for title in content]

with open('./weibo_search.csv','w') as csvfile:
    head=['排行','链接','标题']
    writer=csv.writer(csvfile)
    writer.writerow(head)
    for num in range(len(ids)):
        row=[]
        row.append(ids[num])
        row.append(links[num])
        row.append(titles[num])
        writer.writerow(row)
